package com.ruoyi.yyq.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.yyq.domain.CarMerchantBalance;
import com.ruoyi.yyq.domain.CarVerificationRecord;
import com.ruoyi.yyq.service.ICarMerchantBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.yyq.domain.CarWithdrawal;
import com.ruoyi.yyq.service.ICarWithdrawalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.utils.DateUtils;
/**
 * 商户提现记录Controller
 * 
 * @author 尹永青
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/withdrawal")
@CrossOrigin
public class CarWithdrawalController extends BaseController
{
    @Autowired
    private ICarWithdrawalService carWithdrawalService;
    @Autowired
    private ICarMerchantBalanceService carMerchantBalanceService;

    /**
     * 查询商户提现记录列表
     */
    @RequiresPermissions("yyq:withdrawal:list")
    @GetMapping("/list")
    public TableDataInfo list(CarWithdrawal carWithdrawal)
    {
        startPage();
        carWithdrawal.setStatus(1L);
        List<CarWithdrawal> list = carWithdrawalService.selectCarWithdrawalList(carWithdrawal);
        return getDataTable(list);
    }

    @RequiresPermissions("yyq:record:list")
    @GetMapping("/listByMid/{mid}")
    public AjaxResult listByMid(@PathVariable("mid") Long mid){
        CarWithdrawal carWithdrawal = new CarWithdrawal();
        carWithdrawal.setMerchantId(mid);
        List<CarWithdrawal> carWithdrawals = carWithdrawalService.selectCarWithdrawalList(carWithdrawal);
        return success(carWithdrawals);
    }
    /**
     * 导出商户提现记录列表
     */
    @RequiresPermissions("yyq:withdrawal:export")
    @Log(title = "商户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarWithdrawal carWithdrawal)
    {
        List<CarWithdrawal> list = carWithdrawalService.selectCarWithdrawalList(carWithdrawal);
        ExcelUtil<CarWithdrawal> util = new ExcelUtil<CarWithdrawal>(CarWithdrawal.class);
        util.exportExcel(response, list, "商户提现记录数据");
    }

    /**
     * 获取商户提现记录详细信息
     */
    @RequiresPermissions("yyq:withdrawal:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carWithdrawalService.selectCarWithdrawalById(id));
    }

    /**
     * 新增商户提现记录
     */
    @RequiresPermissions("yyq:withdrawal:add")
    @Log(title = "商户提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarWithdrawal carWithdrawal)
    {
        carWithdrawal.setWithdrawalTime(DateUtils.getNowDate());
        return toAjax(carWithdrawalService.insertCarWithdrawal(carWithdrawal));
    }

    /**
     * 修改商户提现记录
     */
    @RequiresPermissions("yyq:withdrawal:edit")
    @Log(title = "商户提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarWithdrawal carWithdrawal)
    {
        //当审批通过时，更新商户余额
        if (carWithdrawal.getStatus() == 2) {
            CarMerchantBalance balance = carMerchantBalanceService.selectCarMerchantBalanceByMid(carWithdrawal.getMerchantId());
            System.out.println(balance);
            BigDecimal initialBalance = balance.getInitialBalance();
            BigDecimal subtract = initialBalance.subtract(BigDecimal.valueOf(carWithdrawal.getCashWithdrawal()));
            balance.setInitialBalance(subtract);
            carMerchantBalanceService.updateCarMerchantBalance(balance);
        }
        return toAjax(carWithdrawalService.updateCarWithdrawal(carWithdrawal));
    }

    /**
     * 删除商户提现记录
     */
    @RequiresPermissions("yyq:withdrawal:remove")
    @Log(title = "商户提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carWithdrawalService.deleteCarWithdrawalByIds(ids));
    }
}
